package com.liuyk.commonlib.help;

import android.content.Context;

import com.liuyk.commonlib.utility.DeviceUtils;
import com.liuyk.commonlib.utility.FileUtils;

import java.io.File;

/**
 * 更新逻辑处理
 * <p>
 * Created by liuyakui on 2020/7/3
 */
public class UpdateHelp {
    public static final String SUFFIX_APK = ".apk";

    public static String getApkName(Context context, String mVersionName) {
        return DeviceUtils.getAppName(context) + mVersionName;
    }

    public static File getApkCacheFile(Context context, String mVersionName) {
        File apkDir = getApkDir();
        File[] files = apkDir.listFiles();
        if (files == null || files.length == 0) {
            return null;
        }
        String apkName = getApkName(context, mVersionName) + SUFFIX_APK;
        for (File f : files) {
            if (apkName.equals(f.getName())) {//包含后缀，如果有
                return f;
            }
        }
        return null;
    }

    public static File getApkDir() {
        File apk = new File(FileUtils.getFileDir(), "apk");
        if (!apk.exists()) {
            apk.mkdirs();
        }
        return apk;
    }

    public static void delApk() {
        File apkDir = getApkDir();
        File[] files = apkDir.listFiles();
        if (files == null || files.length == 0) {
            return;
        }
        for (File f : files) {
            f.delete();
        }
    }

    public static File getApkDownloadFile(Context context, String mVersionName) {
        return new File(getApkDir(), getApkName(context, mVersionName));
    }
}
